package cn.aran.controller;

import cn.aran.domain.User;
import cn.aran.handler.UserFallBackHandler;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RefreshScope  //刷新配置
@RestController
public class UserController {
    @Value("${test}")
    private Long test;

    @GetMapping("/getUserById/{id}")
    @SentinelResource(value = "getUserById" ,
            blockHandler = "getUserByIdBlockHandler",
            blockHandlerClass = UserFallBackHandler.class,
            fallback = "getUserByIdFallBack")
    public User getUserById(@PathVariable Long id){
        // int i = 1 / 0;
        return User.builder().id(test).intro("测试").name("华华").build();
    }


    public static User getUserByIdFallBack(@PathVariable Long id , Throwable t){
        return User.builder().id(-2L).intro("fallback").name("fallback").build();
    }
}
